﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.CreditCards;

namespace Application.MainModule.CreditCardManagement
{
    public class CreditCardManagementService : ICreditCardManagementService, IDisposable
    {
        #region private properties

        private ICreditCardRepository CreditCardRepository
        {
            get;
            set;
        }

        private ICreditCardRepository CreditCardTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for CreditCardService.
        /// </summary>
        /// <param name="creditCardRepository">CreditCardRepository dependency, usually this is resolved by IoC</param>
        public CreditCardManagementService(ICreditCardRepository creditCardRepository)
        {
            if (creditCardRepository == (ICreditCardRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.CreditCardRepository = creditCardRepository;
        }

        #endregion

        #region ICreditCardService implementations

        public void AddCreditCard(CreditCard creditCard)
        {
            IUnitOfWork unitOfWork = this.CreditCardRepository.UnitOfWork as IUnitOfWork;
            this.CreditCardRepository.Add(creditCard);
            unitOfWork.Commit();
        }

        public void ChangeCreditCard(CreditCard creditCard)
        {
            IUnitOfWork unitOfWork = this.CreditCardRepository.UnitOfWork as IUnitOfWork;
            this.CreditCardRepository.Modify(creditCard);
            unitOfWork.CommitAndRefreshChanges();
        }

        public CreditCard FindCreditCardById(Guid creditCardId)
        {
            CreditCardIdSpecification creditCardIdSpecification = new CreditCardIdSpecification(creditCardId);
            return this.CreditCardRepository.FindCreditCard(creditCardIdSpecification);
        }

        public List<CreditCard> FindCreditCardByUserId(Guid userId)
        {
            CreditCardUserIdSpecification creditCardUserIdSpecification = new CreditCardUserIdSpecification(userId);
            return this.CreditCardRepository.FindCreditCards(creditCardUserIdSpecification).ToList<CreditCard>();
        }

        public void RemoveCreditCard(CreditCard creditCard)
        {
            if (creditCard == (CreditCard)null)
                throw new ArgumentNullException("creditCard");

            IUnitOfWork unitOfWork = this.CreditCardRepository.UnitOfWork as IUnitOfWork;
            this.CreditCardRepository.Remove(creditCard);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.CreditCardRepository != null
                &&
                this.CreditCardRepository.UnitOfWork != null)
            {
                this.CreditCardRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
